package com.yuxin.common.security;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author zehui.zeng
 * @date 13-3-22 上午10:36
 */
public class CharsetEncodingFilter implements Filter {
    private String charset;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
         this.charset = filterConfig.getInitParameter("charset");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 设置请求响应字符编码
        servletResponse.setCharacterEncoding(charset);
        servletResponse.setContentType("text/html;charset=UTF-8");
        servletRequest.setCharacterEncoding(charset);
        // 传递控制到下一个过滤器
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        charset = null;
    }
}
